package com.snake.excel2java;

import com.snake.excel2java.excel.blindbox.OpenBlindBox;
import com.snake.excel2java.excel.common.ext.file.DropExt;
import com.snake.excel2java.excel.common.ext.row.DropRowExt;
import com.snake.excel2java.excel.common.ext.row.GroupRowExt;
import com.snake.excel2java.excel.generator.ExcelGenerator;
import com.snake.excel2java.excel.service.ExcelLoadService;
import com.snake.excel2java.excel.type.DropType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

import java.io.File;
import java.util.*;

@Slf4j
@SpringBootApplication
@ComponentScan(basePackages= {"com.snake.excel2java.*"})
public class Excel2javaApplication implements CommandLineRunner {

	@Autowired
	ExcelGenerator excelGenerator;

	@Autowired
	ExcelLoadService excelLoadService;

	@Autowired
	DropExt dropExt;

	@Autowired
	OpenBlindBox openBlindBox;

	public static void main(String[] args)  {
		SpringApplication.run(Excel2javaApplication.class, args);
	}

	@Override
	public void run(String... args) throws Exception {

		excelGenerator.generate();
//		loadExcelsTest();
//		dropTest();
	}

	private  void loadExcelsTest() {

		File dir = new File("");

		dir = dir.getAbsoluteFile();
		dir = dir.getParentFile();

		String excelPath = dir.getAbsolutePath() + "/excels/";
		String excelTempPath = dir.getAbsolutePath() + "/excels/temp/";

		excelLoadService.startTest(excelPath,
				excelTempPath,
				null,
				null);

	}

	private void dropTest() {

		List<GroupRowExt> list = new LinkedList<>();
		HashMap<Integer, Integer> map = new HashMap<>();
		Set<Integer> execludeShopIdSet = new HashSet<>();
		for(Map.Entry<Integer, DropRowExt> entry: dropExt.getDataMap().entrySet()) {

			DropRowExt row = entry.getValue();

			list = new LinkedList<>();
			map = new HashMap<>();
			for(int i = 0; i < 10000; ++i) {
				list = openBlindBox.dropTest(1l, DropType.one, row.getGroupId());
				updateMap(map, list);
			}


			log.info("groupId: {} drop 1 {}, {}",  row.getGroupId(), 1, map);


			list = new LinkedList<>();
			map = new HashMap<>();
			for(int i = 0; i < 3500; ++i) {
				list = openBlindBox.dropTest(1l, DropType.three, row.getGroupId());
				updateMap(map, list);
			}

			log.info("groupId: {} drop 3  {}, {}",  row.getGroupId(), 3, map);


			list = new LinkedList<>();
			map = new HashMap<>();
			for(int i = 0; i < 1000; ++i) {
				list = openBlindBox.dropTest(1l, DropType.ten, row.getGroupId());
				updateMap(map, list);
			}

			log.info("groupId: {} drop 10 {}, {}",  row.getGroupId(), 10, map);


		}

	}



	private void updateMap(HashMap<Integer, Integer> map, List<GroupRowExt> list) {
		for(GroupRowExt row: list) {
			int shopId = row.getShopId();
			Integer count = map.get(shopId);
			if(null == count) {
				count = 1;
			} else {
				++count;
			}
			map.put(shopId, count);
		}
	}
}
